package com.ling.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ling.bean.Admin;
import com.ling.bean.Msg;
import com.ling.bean.Notification;
import com.ling.service.NotificationService;
import com.ling.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class AdminNotificationController {

    private final NotificationService notificationService;

    @Autowired
    public AdminNotificationController(NotificationService notificationService) {
        this.notificationService = notificationService;
    }

    //跳转到查看公告页面,管理员用户
    @RequestMapping(value = "/adminViewNotification/{id}",method = RequestMethod.GET)
    public String goToAdminViewNotification(@PathVariable String id,@Autowired HttpServletRequest request) {
        request.setAttribute("notificationId", id);
        return "/admin/adminViewNotification";
    }

    //查询指定id的公告数据，不区分用户
    @RequestMapping(value = "/getNotification",method = RequestMethod.GET)
    @ResponseBody
    public Msg getNotification(@RequestParam String id) {
        Notification notification = notificationService.getNotificationById(id);
        return Msg.success().add("notification",notification);
    }

    //删除指定id的公告，需要验证管理员账号
    @RequestMapping(value = "/delNotification",method = RequestMethod.GET)
    @ResponseBody
    public Msg delNotification(HttpSession session, @RequestParam String id) {
        //验证管理员账号
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            return Msg.fail().add("error", "session失效，请重新登录后重试！");
        }
        //开始删除
        notificationService.delNotification(id);
        return Msg.success();
    }

    //查询所有公告，并按照分页，创建时间逆序排序返回json,不需要验证管理员身份
    @RequestMapping(value = "/allNotification/{pn}", method = RequestMethod.GET)
    @ResponseBody
    public Msg getAllNotification(@PathVariable Integer pn) {
        //开始分页
        PageHelper.startPage(pn, 15);
        List<Notification> notifications = notificationService.getAllNotification();
        PageInfo<Notification> pageInfo = new PageInfo<>(notifications);
        return Msg.success().add("pageInfo", pageInfo);
    }

    //跳转到添加公告页面
    @RequestMapping(value = "/adminAddNotification", method = RequestMethod.GET)
    public String goToAdminAddNotification(@Autowired HttpSession session) {
        if (session.getAttribute("admin") == null) {
            return "redirect:/adminLogin";
        }
        //登录查询
        return "/admin/adminAddNotification";
    }


    //添加公告,管理员用户
    @RequestMapping(value = "/adminAddNotification", method = RequestMethod.POST)
    @ResponseBody
    public Msg adminAddNotification(Notification notification, @Autowired HttpSession session) {
        //管理员登录
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            return Msg.fail().add("error", "session失效，请重新登录后重试！");
        }
        notification.setId(StringUtil.getRandomString(16));
        notification.setAdminid(admin.getId());
        notification.setEdittime(new Date());
        //保存该公告
        notificationService.saveNotification(notification);
        return Msg.success();
    }

    @RequestMapping(value = "/adminNotification", method = RequestMethod.GET)
    public String goToAdminNotification() {
        return "/admin/adminNotification";
    }
}
